home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Memory / PointerTo.h < prev   
Encoding:
Text File  |  1997-06-28  |  1.1 KB  |  57 lines  |  [TEXT/CWIE]

  1. // PointerTo.h
  2.  
  3. #ifndef PointerTo_h
  4. #define PointerTo_h
  5.  
  6. #ifndef Integers_h
  7. #include "Integers.h"
  8. #endif
  9. #ifndef Assert_h
  10. #include "Assert.h"
  11. #endif
  12.  
  13. template < class T >
  14. class PointerTo
  15.   {
  16.     private:
  17.         T* pointer;
  18.     
  19.         PointerTo( const PointerTo<T>& r );            // These methods intertionally left unimplemented.
  20.         void operator=( const PointerTo<T>& r );
  21.         
  22.     public:
  23.         PointerTo( T* p = 0 )    : pointer( p )        {}
  24.         ~PointerTo()
  25.           {
  26.             if ( pointer != 0 )
  27.                 delete pointer;
  28.           }
  29.         
  30.         bool Null() const                { return pointer == 0; }
  31.         operator==( T* p ) const    { return pointer == p; }
  32.  
  33.         operator T*() const            { return pointer; }
  34.         
  35.         T& operator*() const            { Assert( pointer != 0 ); return *pointer; }
  36.         T* operator->() const        { Assert( pointer != 0 ); return pointer; }
  37.         
  38.         void operator=( T* p )        { Assert( pointer == 0 ); pointer = p; }
  39.         void Dispose()                    { Assert( pointer != 0 ); delete pointer; pointer = 0; }
  40.         
  41.         void Swap( PointerTo<T>& r )
  42.           {
  43.             T* temp( r.pointer );
  44.             r.pointer = pointer;
  45.             pointer = temp;
  46.           }
  47.         
  48.         void operator<<( PointerTo<T>& r )
  49.           {
  50.             Assert( pointer == 0 );
  51.             pointer = r.pointer;
  52.             r.pointer = 0;
  53.           }
  54.   };
  55.  
  56. #endif
  57.